package org.jeecg.modules.demo.mall.yxstorecouponuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.mall.entity.YxStoreCouponUser;
import org.jeecg.modules.demo.mall.yxstorecouponuser.mapper.YxStoreCouponUserMapper;
import org.jeecg.modules.demo.mall.yxstorecouponuser.service.IYxStoreCouponUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 优惠券记录表
 * @Author: jeecg-boot
 * @Date: 2023-11-09
 * @Version: V1.0
 */
@Service
public class YxStoreCouponUserServiceImpl extends ServiceImpl<YxStoreCouponUserMapper, YxStoreCouponUser> implements IYxStoreCouponUserService {

    @Override
    public List<YxStoreCouponUser> firstQuery(List<YxStoreCouponUser> yxStoreCouponUserList, BigDecimal useMinPrice) {
        //计算优惠力度最大的
        ArrayList<YxStoreCouponUser> bigDecimals = new ArrayList<>();

        Map<BigDecimal, List<YxStoreCouponUser>> decimalMap = yxStoreCouponUserList.stream().filter(yxStoreCouponUser1 -> yxStoreCouponUser1.getCouponType()!=3&&yxStoreCouponUser1.getIsUsage().equals(0)).map(yxStoreCouponUser -> {
            //如果是满减券的话进行扣减
            if (yxStoreCouponUser.getCouponType().equals(0)) {
                yxStoreCouponUser.setMaxPrice(useMinPrice.subtract(yxStoreCouponUser.getDiscountPrice()));
            }
            if (yxStoreCouponUser.getUserCouponType().equals(1)) {
                yxStoreCouponUser.setMaxPrice(useMinPrice.multiply(yxStoreCouponUser.getDiscountPrice()));
            }
//            yxStoreCouponUser.setMaxPrice(BigDecimal.valueOf());
            return yxStoreCouponUser;
        }).collect(Collectors.groupingBy(YxStoreCouponUser::getMaxPrice));
        //获取去值最大的键
        BigDecimal min = Collections.min(decimalMap.keySet());
        //获取最优惠的字段
        List<YxStoreCouponUser> yxStoreCouponUsers = decimalMap.get(min);
        YxStoreCouponUser yxStoreCouponUser = yxStoreCouponUsers.get(0);
        for (int i = 0; i < yxStoreCouponUserList.size(); i++) {
            if (yxStoreCouponUserList.get(i).getId().equals(yxStoreCouponUser.getId())) {
                if (i != 0) {
                    YxStoreCouponUser item = bigDecimals.get(0);
                    bigDecimals.set(0, yxStoreCouponUserList.get(i));
                    bigDecimals.add(item);
                }
                bigDecimals.add(yxStoreCouponUser);
                continue;
            }
            bigDecimals.add(yxStoreCouponUserList.get(i));

        }

        return bigDecimals;
    }










    @Override
    public void updateExpireDate(Integer uid) {
        //过期的数据进行修改
        LambdaQueryWrapper<YxStoreCouponUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(YxStoreCouponUser::getUid, uid);
        lambdaQueryWrapper.isNotNull(YxStoreCouponUser::getEndTime);
        //过期的修改
        List<YxStoreCouponUser> yxStoreCouponUsers = this.list(lambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(yxStoreCouponUsers)) {
            yxStoreCouponUsers.forEach(yxStoreCouponUser -> {
                if (yxStoreCouponUser.getEndTime().compareTo(new Date()) < 0) {
                    yxStoreCouponUser.setStatus(2);
                }
            });
            this.updateBatchById(yxStoreCouponUsers);
        }
    }
}
